using System;
using Engine;
using Engine.Graphics;
using Game;

namespace RuthlessConquest {
    public class MessageDialog : Dialog {
        public Action<MessageDialogButton> Handler;
        public LabelWidget LargeLabelWidget;
        public LabelWidget SmallLabelWidget;
        public ButtonWidget Button1Widget;
        public ButtonWidget Button2Widget;

        public bool AutoHide { get; set; }

        public MessageDialog(string largeMessage,
            string smallMessage,
            string button1Text,
            string button2Text,
            Vector2 size,
            Action<MessageDialogButton> handler) {
            Handler = handler;
            HorizontalAlignment = WidgetAlignment.Center;
            VerticalAlignment = WidgetAlignment.Center;
            Size = new Vector2(size.X >= 0f ? size.X : 600f, size.Y >= 0f ? size.Y : 250f);
            ClampToBounds = true;
            Widget[] obj = [
                new RectangleWidget { FillColor = Colors.Back, OutlineColor = Colors.ForeDim, OutlineThickness = 2f }, new InterlaceWidget(), null
            ];
            StackPanelWidget panel1 = new() {
                Direction = LayoutDirection.Vertical,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Near,
                Margin = new Vector2(10f, 10f)
            };
            panel1.Children.Add(
                LargeLabelWidget = new LabelWidget {
                    HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Near, Margin = new Vector2(0f, 5f)
                }
            );
            ScrollPanelWidget scrollPanel = new() { Direction = LayoutDirection.Vertical, Margin = new Vector2(20f, 5f) };
            scrollPanel.Children.Add(
                SmallLabelWidget = new LabelWidget {
                    FontScale = 0.75f,
                    Color = Colors.ForeDim,
                    HorizontalAlignment = WidgetAlignment.Center,
                    VerticalAlignment = WidgetAlignment.Near,
                    TextAnchor = TextAnchor.HorizontalCenter,
                    WordWrap = true
                }
            );
            panel1.Children.Add(scrollPanel);
            StackPanelWidget obj4 = new() {
                Direction = LayoutDirection.Horizontal,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Far,
                Margin = new Vector2(0f, 5f)
            };
            obj4.Children.Add(
                Button1Widget = new BevelledButtonWidget { Size = new Vector2(200f, 60f), Margin = new Vector2(40f, 0f), Sound = "RCSounds/Click2" }
            );
            obj4.Children.Add(
                Button2Widget = new BevelledButtonWidget { Size = new Vector2(200f, 60f), Margin = new Vector2(40f, 0f), Sound = "RCSounds/Click2" }
            );
            panel1.Children.Add(obj4);
            obj[2] = panel1;
            Children.Add(obj);
            LargeLabelWidget.IsVisible = !string.IsNullOrEmpty(largeMessage);
            LargeLabelWidget.Text = largeMessage ?? string.Empty;
            SmallLabelWidget.IsVisible = !string.IsNullOrEmpty(smallMessage);
            SmallLabelWidget.Text = smallMessage ?? string.Empty;
            Button1Widget.IsVisible = !string.IsNullOrEmpty(button1Text);
            Button1Widget.Text = button1Text ?? string.Empty;
            Button2Widget.IsVisible = !string.IsNullOrEmpty(button2Text);
            Button2Widget.Text = button2Text ?? string.Empty;
            if (!Button1Widget.IsVisible
                && !Button2Widget.IsVisible) {
                throw new InvalidOperationException(LanguageControl.Get("RCMessageDialog", "1"));
            }
            AutoHide = true;
        }

        public MessageDialog(string largeMessage, string smallMessage, string button1Text, string button2Text, Action<MessageDialogButton> handler) :
            this(largeMessage, smallMessage, button1Text, button2Text, new Vector2(-1f), handler) { }

        public override void Update() {
            if (Button1Widget.IsClicked) {
                Dismiss(MessageDialogButton.Button1);
            }
            else if (Button2Widget.IsClicked) {
                Dismiss(MessageDialogButton.Button2);
            }
        }

        public void Dismiss(MessageDialogButton button) {
            if (AutoHide) {
                DialogsManager.HideDialog(this);
            }
            Handler?.Invoke(button);
        }
    }
}